웹 서비스의 연락처를 나열하고 스크린 샷과 같이 '섹션화 된'tableView 연락처에 표시합니다. 문제는 섹션 A와 섹션 S에 대한 첫 번째 행의 확인란에 대해 동일한 태그 값을 얻는 것입니다. 하나의 배열을 정렬하고 인덱싱 된 테이블보기에 표시했습니다. 표시된 섹션 수에 관계없이 indexPath.row에 따라 다른 태그 값을 가져 오는 방법. 내가 시도한 것입니다. cellForRowAtIndexPath에서 : UIButton * checkBox; if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPhone) { checkBox = [[UIButton alloc] initWithFrame : CGRectMake (7, 8, 30, 30)]; } 그밖에 { checkBox = [[UIButton alloc] initWithFrame : CGRectMake (15, 13, 30, 30)]; } // int cnt = 0; // for (int i = indexPath.section-1; i> 0; i--) // { // cnt + = [[objectsForCharacters objectForKey : [arrayOfCharacters objectAtIndex : i]] count]; // arrayOfCharachters는 'Z'에 'A'문자를가집니다. //} //checkBox.tag = cnt + indexPath.row; [checkBox setImage : [UIImage imageNamed : @ "checkBox.png"] forState : UIControlStateNormal]; [checkBox addTarget : self action : @selector (checkBoxClicked :) forControlEvents : UIControlEventTouchUpInside]; [checkBox setTag : indexPath.row]; [cell.contentView addSubview : checkBox]; 리턴 셀; } -(void) checkBoxClicked : (id) sender { CGPoint buttonPosition = [보낸 사람 convertPoint : CGPointZero toView : self.tableViewContact]; NSIndexPath * indexPath = [self.tableViewContact indexPathForRowAtPoint : buttonPosition]; UIButton * tappedButton = (UIButton *) 보낸 사람; NSLog (@ "태그 번호 = % d", [발신자 태그]); if ([tappedButton.currentImage isEqual : [UIImage imageNamed : @ "checkBox.png"]]) { [sender setImage : [UIImage imageNamed : @ "checkBoxMarked.png"] forState : UIControlStateNormal]; if (indexPath! = Nil) { NSString * finalIntId = [mutableArrayOfIds objectAtIndex : indexPath.row]; // mutableArrayOfIds에 체크 박스 ID 저장 NSLog (@ "태그 된 체크 버튼 ID = % @", finalIntId); [arrayOfIds addObject : finalIntId]; } // NSString * finalIntId = [mutableArrayOfIds objectAtIndex : tappedButton.tag]; // NSString * finalIntId = [mutableArrayOfIds objectAtIndex : indexPath.row]; } 그밖에 { [발신자 setImage : [UIImage imageNamed : @ "checkBox.png"] forState : UIControlStateNormal]; NSLog (@ "UnChecked"); // [arrayOfIds removeObjectAtIndex : tappedButton.tag]; } } -(NSString *) tableView : (UITableView *) tableView titleForHeaderInSection : (NSInteger) section { if ([arrayOfCharacters count] == 0) { 반환 @ ""; } return [NSString stringWithFormat : @ "% @", [arrayOfCharacters objectAtIndex : section]]; } -(NSArray *) sectionIndexTitlesForTableView : (UITableView *) tableView { NSArray * toBeReturned = [NSArray arrayWithArray : [@ "A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | # " componentsSeparatedByString : @ "|"]]; return toBeReturned; } -(NSInteger) tableView : (UITableView *) tableView sectionForSectionIndexTitle : (NSString *) title atIndex : (NSInteger) index { NSInteger 개수 = 0; for (NSString * character in arrayOfCharacters) { if ([character isEqualToString : title]) { 반환 횟수; } 카운트 ++; } 반환 0; } -(NSInteger) numberOfSectionsInTableView : (UITableView *) tableView { return [arrayOfCharacters count]; // return 1; } -(NSInteger) tableView : (UITableView *) tableView numberOfRowsInSection : (NSInteger) 섹션 { // return [mutableArray count]; return [[objectsForCharacters objectForKey : [arrayOfCharacters objectAtIndex : section]] 개수]; }
2020-12-07 22:55:18
행이 같은 모든 섹션에 대해 동일한 태그 값을 설정하고 있습니다. indexPath에는 {section, row}라는 두 가지 속성이 있습니다. 섹션 A에 두 개의 행이 있다고 가정 해 보겠습니다. 행 1-> indexPath.section = 0, indexPath.row = 0; row2-> indexPath.section = 0의 경우 indexPath.row = 1; 섹션 S에 두 개의 행이 있다고 가정 해 보겠습니다. 행 1-> indexPath.section = 1, indexPath.row = 0; row2-> indexPath.section = 1의 경우 indexPath.row = 1; 따라서 섹션 A의 row1과 섹션 S의 row1에 대해 동일한 태그 값을 0으로 설정하고 있는데 문제가 있습니다. 아래와 같이 태그 값을 설정해보십시오. button.tag = indexPath.section * 1000 + indexPath.row; 섹션과 행을 검색 할 때 NSInteger 섹션 = (button.tag) / 1000; NSInteger 행 = (button.tag) % 1000; | 이 시도... -(UITableViewCell *) tableView : (UITableView *) tableView cellForRowAtIndexPath : (NSIndexPath *) indexPath { static NSString * CellIdentifier = @ "Cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier : CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle : UITableViewCellStyleDefault restartIdentifier : CellIdentifier]; } UIButton * checkBox = [[UIButton 할당] initWithFrame : CGRectMake (280, 10, 50, 44)]; checkBox.backgroundColor = [UIColor orangeColor]; [checkBox addTarget : self action : @selector (checkBoxClicked : event :) forControlEvents : UIControlEventTouchUpInside]; [checkBox setTag : indexPath.row]; [cell.contentView addSubview : checkBox]; 리턴 셀; } -(void) checkBoxClicked : (id) sender 이벤트 : (id) event { NSSet * touches = [이벤트 allTouches]; UITouch * touch = [모든 객체를 만집니다]; CGPoint currentTouchPosition = [touch locationInView : self.tv]; // 여기서 tv는 TableView 개체입니다. NSIndexPath * indexPath = [self.tv indexPathForRowAtPoint : currentTouchPosition]; NSLog (@ "value of indePath.section % d, indexPath.row % d", indexPath.section, indexPath.row); } | 이것은 섹션의 독립적 인 버튼에 태그를 할당하기 때문에 발생합니다. 섹션 A와 S의 첫 번째 행은 모두 행 = 0입니다. 따라서 해당 버튼에 할당 된 태그는 0입니다. 섹션에 대한 참조 유지를 할당해야합니다. Section, Row를 포함하는 쉼표로 구분 된 형식으로 접근성 힌트를 할당하는 것이 좋습니다. 그리고 당신의 방법으로 -(void) checkBoxClicked : (id) sender { // 쉼표로 구분 된 형식에서 문자열을 선택합니다. 첫 번째는 섹션이고 두 번째는 행입니다. } 두 번째 옵션은 당신이하는 일을하고 이와 같은 Button 메서드를 구현하는 것입니다. CGPoint buttonPosition = [송신자 convertPoint : CGPointZero toView : self.tableView]; NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint : buttonPosition]; if (indexPath! = nil) { ... indexpath.section은 섹션이고 index path.row는 행입니다. } 세 번째 옵션도 있습니다. cellforRowAtIndexpath에서 Button에 제목을 지정하십시오. -(UITableViewCell *) tableView : (UITableView *) tableView cellForRowAtIndexPath : (NSIndexPath *) indexPath [btn setTitle : <# Your Row #> forState : UIControlStateDisabled]; [btn setTag <#Your Section #>]; 따라서 버튼 방법을 수신하면 섹션 (태그) 및 행 (사용 안 함 상태의 제목)을 모두 가질 수 있습니다. -(void) checkBoxClicked : (id) sender {[button titleForState : UIControlStateDisabled]; // 당신의 행 button.tag // 내 섹션} | 이 시도! 각 섹션은 섹션 수를 알 수 있으며 개별 행 수를 추가합니다. [[fieldTitlesList objectAtIndex : indexPath.section-1] count] + indexPath.row 여기서 fieldTitlesList는 섹션의 배열입니다. | 내 문제를 해결 한 다음 코드를 추가했습니다. NSInteger rowNumber = 0; for (NSInteger i = 0; i